﻿using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

using X.Config;
using X.Entity;
using X.Common.VerifyImage;
using X.Common;

namespace E.Web.Admin.UI
{
    public class VerifyImagePage : System.Web.UI.Page
    {

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="e"></param>
        override protected void OnInit(EventArgs e)
        {
            //
            base.OnInit(e);

            GeneralConfigInfo config = GeneralConfigs.GetConfig();

            string bgcolor = XRequest.GetQueryString("bgcolor").Trim();
            int textcolor = XRequest.GetQueryInt("textcolor", 1);
            string[] bgcolorArray = bgcolor.Split(',');

            Color bg = Color.White;

            if (bgcolorArray.Length == 1 && bgcolor != string.Empty)
            {
                bg = Utils.ToColor(bgcolor);
            }
            else if (bgcolorArray.Length == 3 && Utils.IsNumericArray(bgcolorArray))
            {
                bg = Color.FromArgb(Utils.StrToInt(bgcolorArray[0], 255), Utils.StrToInt(bgcolorArray[1], 255), Utils.StrToInt(bgcolorArray[2], 255));
            }

            VerifyImage2 v2 = new VerifyImage2();
            v2.FontSize = config.ValidateCodeFontSize;

            string validateCode = Utils.CreateAuthStr(config.ValidateCodeLength);

            //将验证码写入cookie
            Utils.WriteCookie("vcode", validateCode);

            VerifyImageInfo verifyimg = v2.CreateImage(validateCode, config.ValidateCodeDir, config.ValidateCodeMultValue, config.ValidateCodePhase);

            Bitmap image = verifyimg.Image;

            System.Web.HttpContext.Current.Response.ContentType = verifyimg.ContentType;

            //MemoryStream ms = new MemoryStream();

            image.Save(this.Response.OutputStream, verifyimg.ImageFormat);
            //System.Web.HttpContext.Current.Response.OutputStream.Write(ms.ToArray(), 0, (int)ms.Length);
        }
    }
}
